﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using ProjetoComperio.BusinessLogicLayer;
namespace ProjetoComperio.Presentation.Web.Controllers
{
    public class RssFeedController : Controller
    {
        private ContributionBLL contributionBLL;
        private EventBLL eventBLL;

        public RssFeedController()
        {
            contributionBLL = new ContributionBLL();
            eventBLL = new EventBLL();
        }

        [HttpGet]
        public ActionResult SearchContributions(string keyword, int rows)
        {
            var stringBuilder = new StringBuilder();
            XmlWriter xmlWriter = XmlWriter.Create(stringBuilder);
            xmlWriter.WriteStartElement("contributions");
            foreach (var contribution in contributionBLL.SearchContributions(keyword, rows).ToList())
            {
                xmlWriter.WriteStartElement("contribution");
                xmlWriter.WriteElementString("contributionid", contribution.ContributionID.ToString());
                xmlWriter.WriteElementString("title", contribution.Title);
                xmlWriter.WriteElementString("description", contribution.Description);
                xmlWriter.WriteElementString("registerdate", contribution.RegisterDate.ToShortDateString());
                xmlWriter.WriteElementString("isevent", (eventBLL.RetrieveEvent(contribution.ContributionID) != null).ToString());
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndElement();
            xmlWriter.Flush();
            return new ContentResult
            {
                ContentType = "text/xml",
                Content = stringBuilder.ToString(),
                ContentEncoding = System.Text.Encoding.UTF8
            };

        }

        [HttpGet]
        public ActionResult RetrieveContribution(int id)
        {
            var contribution = contributionBLL.RetrieveContribution(id);
            var stringBuilder = new StringBuilder();
            XmlWriter xmlWriter = XmlWriter.Create(stringBuilder);
            xmlWriter.WriteStartElement("contributions");
            if (contribution != null)
            {
                xmlWriter.WriteStartElement("contribution");
                xmlWriter.WriteElementString("contributionid", contribution.ContributionID.ToString());
                xmlWriter.WriteElementString("title", contribution.Title);
                xmlWriter.WriteElementString("description", contribution.Description);
                xmlWriter.WriteElementString("registerdate", contribution.RegisterDate.ToShortDateString());
                xmlWriter.WriteElementString("isevent", (eventBLL.RetrieveEvent(contribution.ContributionID) != null).ToString());
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndElement();
            xmlWriter.Flush();
            return new ContentResult
            {
                ContentType = "text/xml",
                Content = stringBuilder.ToString(),
                ContentEncoding = System.Text.Encoding.UTF8
            };

        }

        [HttpGet]
        public ActionResult RetrieveEvent(int id)
        {
            var contribution = eventBLL.RetrieveEvent(id);
            var stringBuilder = new StringBuilder();
            XmlWriter xmlWriter = XmlWriter.Create(stringBuilder);
            xmlWriter.WriteStartElement("contributions");
            if (contribution != null)
            {
                xmlWriter.WriteStartElement("contribution");
                xmlWriter.WriteElementString("contributionid", contribution.ContributionID.ToString());
                xmlWriter.WriteElementString("title", contribution.Title);
                xmlWriter.WriteElementString("description", contribution.Description);
                xmlWriter.WriteElementString("registerdate", contribution.RegisterDate.ToShortDateString());
                xmlWriter.WriteElementString("isevent",
                                             (eventBLL.RetrieveEvent(contribution.ContributionID) != null).ToString());
                xmlWriter.WriteElementString("location", contribution.Location);
                xmlWriter.WriteElementString("author", contribution.Author);
                xmlWriter.WriteElementString("authordescription", contribution.AuthorDescription);
                xmlWriter.WriteElementString("subject", contribution.Subject);
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndElement();
            xmlWriter.Flush();
            return new ContentResult
            {
                ContentType = "text/xml",
                Content = stringBuilder.ToString(),
                ContentEncoding = System.Text.Encoding.UTF8
            };
        }


    }
}
